from dependency_injector.wiring import Provide, inject
from fastapi import APIRouter, Depends, HTTPException
from starlette import status

from business_processes.opening_account_validation.domain.entity.package import AccountPackage
from business_processes.opening_account_validation.application.use_case.command import AccountPackageCommandUseCase
from business_processes.opening_account_validation.presentation.rest.request import AccountPackageRequest
from business_processes.opening_account_validation.presentation.rest.response import PackageResult, AccountPackageResponse
from business_processes.opening_account_validation.domain.exception.account_package import AccountPackageExistsException

from core.infra.container import AppContainer
from core.presentation.response import BaseResponse


router = APIRouter(prefix='/opening_account_validation')


@router.post(
    '/validate_package',
    status_code=status.HTTP_201_CREATED,
    responses={
        status.HTTP_201_CREATED: {'model': AccountPackage},
        status.HTTP_409_CONFLICT: {'model': BaseResponse},
    },
)
@inject
def validate_package(
    package_request: AccountPackageRequest,
    package_processing_command: AccountPackageCommandUseCase = Depends(
        Provide[AppContainer.reception.reservation_command]
    ),
) -> AccountPackageResponse:
    try:
        package: AccountPackage = package_processing_command.execute(
            request=package_request
        )

    except AccountPackageExistsException as e:
        raise HTTPException(
            status_code=status.HTTP_409_CONFLICT,
            detail=e.message,
        )

    return AccountPackageResponse(
        detail='ok',
        result=PackageResult.from_entity(account_package=package),
    )
